import { getColorTextLoadMap, getTenantID, tooltipLength } from '@/utils';
import { Button, Popconfirm, Tooltip, message } from 'antd';
import pageStyles from '@/common/pageLayout.less';
import { Fragment } from 'react';
import { EditOutlined, CloseCircleOutlined } from '@ant-design/icons';
import { history } from 'umi';
import { getKeyRouter } from '@/utils';
import { DictionaryRenderFun } from '../../Acommon';
import moment from 'moment';
import { tableMoreBtnConfig } from '@/utils/myutilConfig';
import { AcceptanceManageAPI } from '../common/type';
import { ILoadMapTypeAll } from '@/common';

/**操作按钮的显示*/
const Operation = (
  curRes: Record<string, any>,
  operateFuns: AcceptanceManageAPI.IMaterialTableActionType,
  record: Record<string, any>,
  leftSelectName: string[],
) => {
  let operationArray: any = [];

  /** 一个按钮都没有时展示*/
  const Disable = (
    <Fragment key={record?.id + '--Disable'}>
      <Button style={{ color: '#000' }} className="manageBtntTxtStyle" type={'link'} size={'small'} disabled>
        --
      </Button>
    </Fragment>
  );

  /** 更多按钮配置 */
  const otherBtnConfig: AcceptanceManageAPI.TableMoreBtnConfigApi[] = [
    {
      icon: <EditOutlined />,
      btnName: '编辑',
      clickFnc: operateFuns,
      clickType: 'onEdit',
      isVisible: curRes?.['/assetMaterial/edit'] && record.assetState !== `${getTenantID()}${ILoadMapTypeAll.MATERIEL_STATUS}1003`,
      // isVisible: true
    },
    {
      icon: <CloseCircleOutlined style={{ color: 'red' }} />,
      btnName: <span style={{ color: 'red' }}>失效</span>,
      clickFnc: operateFuns,
      clickType: 'onCancel',
      isVisible: curRes?.['/assetMaterial/cancel'] && record.assetState !== `${getTenantID()}${ILoadMapTypeAll.MATERIEL_STATUS}1003`,
      // isVisible: true
    },
  ];
  const ohterBtn = tableMoreBtnConfig(otherBtnConfig, record);
  operationArray = ohterBtn;
  operationArray = operationArray?.filter((itemBtn: any) => itemBtn);
  return operationArray && operationArray?.length > 0 ? operationArray : [Disable];
};

/**
 * 表格信息 展示和操作
 * @returns
 */
const getTableColumns = (
  /** 资源权限参数 */
  curRes: Record<string, any>,
  /** 表格操作按钮 */
  operateFuns: AcceptanceManageAPI.IMaterialTableActionType,
  /** 左侧仓库名称 */
  leftSelectName: string[],
) => {
  return [
    {
      title: '材料编码',
      dataIndex: 'materialCode',
      key: 'materialCode',
      width: 120,
      fixed: 'left',
      ellipsis: {
        showTitle: true,
      },
      visible: true,
      unDrag: true,
      sorter: (a: any, b: any) => (a.materialCode > b.materialCode ? 1 : -1),
      render: (text: string) => {
        return (
          <Tooltip title={text}>
            <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
          </Tooltip>
        );
      },
    },
    {
      title: '材料名称',
      dataIndex: 'assetName',
      key: 'assetName',
      width: 120,
      fixed: 'left',
      ellipsis: {
        showTitle: true,
      },
      visible: true,
      unDrag: true,
      sorter: (a: any, b: any) => (a.assetName > b.assetName ? 1 : -1),
      render: (text: string, record: any) => {
        return (
          <>
            {text ? (
              <Tooltip title={text}>
                <a
                  onClick={() => {
                    let obj = JSON.parse(JSON.stringify(record));
                    let objectKeysArr = Object.keys(obj.assetjson);
                    let filterArr = objectKeysArr.filter((item: string) => {
                      return item.substring(item.length - 4) === 'Date';
                    });
                    for (let i = 0; i < filterArr.length; i++) {
                      obj.assetjson[filterArr[i]] = obj.assetjson[filterArr[i]] ? moment(obj.assetjson[filterArr[i]]) : '';
                    }
                    obj.assetjson.hasOwnProperty('warrantyDeadline') &&
                      (obj.assetjson.warrantyDeadline = obj.assetjson.warrantyDeadline ? moment(obj.assetjson.warrantyDeadline) : '');
                    history.push({
                      pathname: `/assetMaterial/detail/:${getKeyRouter()}${getKeyRouter()}`,
                      state: {
                        pageType: 'view',
                        record: obj,
                      },
                    });
                  }}
                >
                  {text}
                </a>
              </Tooltip>
            ) : (
              <>--</>
            )}
          </>
        );
      },
    },
    {
      title: '材料类型',
      dataIndex: 'assetTypeName',
      key: 'assetTypeName',
      width: 110,
      visible: true,
      sorter: (a: any, b: any) => (a.assetTypeName > b.assetTypeName ? 1 : -1),
      render: (text: string) => DictionaryRenderFun({ text, isTooltip: true }),
    },
    {
      title: '总数量',
      dataIndex: 'quantityOrArea',
      key: 'quantityOrArea',
      width: 140,
      visible: true,
      sorter: (a: any, b: any) => (a.quantityOrArea > b.quantityOrArea ? 1 : -1),
      render: (text: string | number) => {
        return (
          <Tooltip title={text}>
            <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
          </Tooltip>
        );
      },
    },
    {
      title: '剩余数量',
      dataIndex: 'residueNum',
      key: 'residueNum',
      width: 140,
      visible: true,
      sorter: (a: any, b: any) => (a.residueNum > b.residueNum ? 1 : -1),
      render: (text: string | number) => {
        return (
          <Tooltip title={text}>
            <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
          </Tooltip>
        );
      },
    },
    {
      title: '报废数量',
      dataIndex: 'cancelNum',
      key: 'cancelNum',
      width: 140,
      visible: true,
      sorter: (a: any, b: any) => (a.cancelNum > b.cancelNum ? 1 : -1),
      render: (text: string | number) => {
        return (
          <Tooltip title={text}>
            <div className={pageStyles['over-flow-hidden']}>{text ? text : text === 0 ? text : '--'}</div>
          </Tooltip>
        );
      },
    },
    {
      title: '材料状态',
      dataIndex: 'assetStateName',
      key: 'assetStateName',
      width: 110,
      visible: true,
      sorter: (a: any, b: any) => (a.assetStateName > b.assetStateName ? 1 : -1),
      render: (text: string) => {
        return (
          <Tooltip title={text}>
            <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
          </Tooltip>
        );
      },
    },
    {
      title: '材料品牌',
      dataIndex: 'brandTypeName',
      key: 'brandTypeName',
      width: 110,
      visible: true,
      sorter: (a: any, b: any) => (a.brandTypeName > b.brandTypeName ? 1 : -1),
      render: (text: string) => DictionaryRenderFun({ text, isTooltip: true }),
    },
    {
      title: '材料来源',
      dataIndex: 'sourceTypeName',
      key: 'sourceTypeName',
      width: 110,
      visible: true,
      sorter: (a: any, b: any) => (a.sourceTypeName > b.sourceTypeName ? 1 : -1),
      render: (text: string) => {
        return (
          <Tooltip title={text}>
            <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
          </Tooltip>
        );
      },
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      key: 'createTime',
      width: 190,
      visible: false,
      sorter: (a: any, b: any) => (a.createTime > b.createTime ? 1 : -1),
      render: (text: string) => {
        return (
          <Tooltip title={text}>
            <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
          </Tooltip>
        );
      },
    },
    {
      title: '创建人',
      dataIndex: 'roleNames',
      key: 'roleNames',
      width: 200,
      visible: false,
      render: (text: string) => {
        return (
          <Tooltip title={text}>
            <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
          </Tooltip>
        );
      },
    },
    // {
    //   title: '材料json',
    //   dataIndex: 'assetJson',
    //   key: 'assetJson',
    //   width: 110,
    //   visible: false,
    //   sorter: (a: any, b: any) => (a.assetJson > b.assetJson ? 1 : -1),
    //   render: (text: string) => {
    //     return (
    //       <Tooltip title={text}>
    //         <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
    //       </Tooltip>
    //     );
    //   },
    // },
    // {
    //   title: '材料图片',
    //   dataIndex: 'assetImage',
    //   key: 'assetImage',
    //   width: 110,
    //   visible: true,
    //   sorter: (a: any, b: any) => (a.assetImage > b.assetImage ? 1 : -1),
    //   render: (text: string) => {
    //     return (
    //       <Tooltip title={text}>
    //         <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
    //       </Tooltip>
    //     );
    //   },
    // },
    {
      title: '材料参数',
      dataIndex: 'paramsInfo',
      key: 'paramsInfo',
      width: 110,
      visible: false,
      sorter: (a: any, b: any) => (a.paramsInfo > b.paramsInfo ? 1 : -1),
      render: (text: string) => {
        return (
          <Tooltip title={text}>
            <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
          </Tooltip>
        );
      },
    },
    {
      title: '折旧方式',
      dataIndex: 'depreciateType',
      key: 'depreciateType',
      width: 110,
      visible: false,
      sorter: (a: any, b: any) => (a.depreciateType > b.depreciateType ? 1 : -1),
      render: (text: string) => {
        return (
          <Tooltip title={text}>
            <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
          </Tooltip>
        );
      },
    },
    {
      title: '材料价值',
      dataIndex: 'assetValue',
      key: 'assetValue',
      width: 110,
      visible: false,
      sorter: (a: any, b: any) => (a.assetValue > b.assetValue ? 1 : -1),
      render: (text: string) => {
        return (
          <Tooltip title={text}>
            <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
          </Tooltip>
        );
      },
    },

    {
      title: '残值率',
      dataIndex: 'scrapRate',
      key: 'scrapRate',
      width: 110,
      visible: false,
      sorter: (a: any, b: any) => (a.scrapRate > b.scrapRate ? 1 : -1),
      render: (text: string) => {
        return (
          <Tooltip title={text}>
            <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
          </Tooltip>
        );
      },
    },
    {
      title: '申请状态',
      dataIndex: 'applyState',
      key: 'applyState',
      width: 110,
      visible: false,
      sorter: (a: any, b: any) => (a.applyState > b.applyState ? 1 : -1),
      render: (text: string) => {
        return (
          <Tooltip title={text}>
            <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
          </Tooltip>
        );
      },
    },
    {
      title: '操作',
      dataIndex: 'operation',
      key: 'operation',
      width: 140,
      visible: true,
      unDrag: true,
      fixed: 'right',
      render: (text: any, record: any) => {
        return (
          <div
            style={{
              display: 'flex',
            }}
          >
            {Operation(curRes, operateFuns, record, leftSelectName)}
          </div>
        );
      },
    },
  ];
};

export { getTableColumns };
